package com.education.dao;

import com.education.pojo.Img;
import com.education.pojo.TestPaper;
import com.education.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Repository
public interface UserMapper {
    int deleteByPrimaryKey(Integer uId);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer uId);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
    /**
     * 邓恒君
     * 个人中心系列功能Dao层
     * Created by Administrator on 2018/4/26.
     */
    //查询用户是否存在
    @Select("select count(*) from user where u_username=#{uUsername}")
    int userCount(@Param("uUsername") String username);

    //根据用户获取对应密保问题
    @Select("select u_question from user where u_username=#{uUsername}")
    @ResultMap("ResultMap")
    User userAnswer(@Param("uUsername") String username);

    //根据用户名和密码查询用户ID
    @Select("select u_id from user where u_username=#{uUsername} and u_password=#{uPassword}")
    @ResultMap("ResultMap")
    User userId(@Param("uUsername") String username, @Param("uPassword") String password);

    //根据用户名和密保答案改变密码
    @Update("update user set u_password=#{uPassword} where u_username=#{uUsername} and u_answer=#{uAnswer}")
    int userPasswordA(User user);

    //根据用户名和原密码改变密码
    @Update("update user set u_password=#{uPassword} where u_username=#{uUsername} and u_password=#{uPasswordOld}")
    int userPasswordP(Map map);

    //添加新用户
    @Insert("Insert into user(u_username,u_password,u_head,u_answer,u_question,u_phone,u_create_time) values(#{uUsername},#{uPassword},#{uHead},#{uAnswer},#{uQuestion},#{uPhone},#{uCreateTime})")
    int addUser(User user);

    //修改用户信息
    @Update("Update user set u_head=#{uHead},u_name=#{uName},u_age=#{uAge},u_sex=#{uSex},u_address=#{uAddress},u_update_time=#{uUpdateTime} where u_id=#{uId}")
    int updateUser(User user);

    //根据用户ID获取个人信息
    @Select("select u_username,u_head,u_name,u_age,u_sex,u_address,u_phone,u_create_time from user where u_id=#{uId}")
    @ResultMap("ResultMap")
    User getUser(@Param("uId") int uid);

    //根据ID修改用户头像
    @Update("update user set u_head=#{uHead} where u_id=#{uId}")
    int changeHead(@Param("uId") int uid, @Param("uHead") String uhead);

    //根据账号密码获取手机号
    @Select("select u_phone from user where u_username=#{uUsername} and u_password=#{uPassword}")
    @ResultMap("ResultMap")
    User getUserPhone(@Param("uUsername") String username, @Param("uPassword") String password);

    //根据手机号获取ID
    @Select("select u_id from user where u_phone=#{uPhone}")
    @ResultMap("ResultMap")
    User getUidPhone(@Param("uPhone") String phone);

    //查询手机号是否存在
    @Select("select count(*) from user where u_phone=#{uPhone}")
    int getPhoneCount(@Param("uPhone") String phone);

    //根据用户ID查询收藏的试卷
    @Select("select * from user_collection uc LEFT JOIN test_paper tp on uc.tp_id=tp.tp_id where uc.u_id=#{uId} GROUP BY tp.tp_name ORDER BY tp.tp_id ASC")
    @ResultMap("testPaperResultMap")
    List<TestPaper> getUserCollection(@Param("uId") int uid);

    //根据用户ID和卷子名删除个人收藏
    @Delete("delete from user_collection where u_id=#{uId} and tp_id=(select tp_id from test_paper where tp_name=#{tpName})")
    int deleteUserCollection(@Param("uId") int uid, @Param("tpName") String tpName);

    //根据试卷名查询对应ID
    @Select("select tp_id from test_paper where tp_name=#{tpName}")
    int selectUserCollection(@Param("tpName") String tpName);

    //根据用户ID查询对应头像
    @Select("select * from img where u_id=#{uId}")
    @ResultMap("ImgResultMap")
    List<Img> getUserImg(@Param("uId") int uId);

    //根据用户ID添加头像
    @Insert("Insert into img(u_id,img_name,img_src) values(#{uId},#{imgName},#{imgSrc})")
    int setUserHead(Img img);

    //根据用户ID和头像src删除头像
    @Delete("delete from img where u_id=#{uId} and img_name=#{imgName}")
    int deleteUserHead(@Param("uId") int uid, @Param("imgName") String imgName);
/*
以下四个为 鑫晨
 */
    Integer selectByTotal();

    Integer selectByToday(@Param("yesterday") Date yesterday, @Param("today") Date today);

    Integer selectByWeek(@Param("monday") Date monday, @Param("sunday") Date sunday);

    Integer selectByMonth(@Param("firstDay") Date firstDay, @Param("lastDay") Date lastDay);
}